<?php

namespace backend\services\active;

use backend\extensions\ApiBaseService;
use backend\models\active\ActiveCouponLink;

/**
 * Desc 优惠券链接管理服务类
 * @author WMX
 */
class ActiveCouponLinkService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        //检索条件
        $condition = ['AND'];
        if ($params['name']) {
            $condition[] = ['like', 'name', $params['name']??''];
        }
        $list = ActiveCouponLink::getDataAll($condition, $params['page'], $params['page_size']);
        foreach($list['list'] as &$value) {
            $value['state_name'] = ActiveCouponLink::$state[$value['state']] ?? '';
            $value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
            $value['end_time'] = date('Y-m-d H:i:s', $value['end_time']);
            $value['page'] = '/pages-active/coupon/index?code='.$value['code'];
        }
        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function create($params)
    {
        $time = date('Y-m-d H:i:s');
        $params['create_time'] = $time;
        $params['update_time'] = $time;
        $params['code'] = mt_rand(1000000, 9999999);
        $params['start_time'] = strtotime($params['time'][0]);
        $params['end_time'] = strtotime($params['time'][1]);
        $params['active_coupon'] = implode(',', $params['active_coupon']);

        unset($params['time']);
        if (ActiveCouponLink::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        $time = date('Y-m-d H:i:s');
        $params['update_time'] = $time;

        $params['start_time'] = strtotime($params['time'][0]);
        $params['end_time'] = strtotime($params['time'][1]);
        $params['active_coupon'] = implode(',', $params['active_coupon']);

        unset($params['id']);
        unset($params['time']);
        unset($params['create_time']);

        if (ActiveCouponLink::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * * */
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (ActiveCouponLink::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail($params)
    {
        $condition = ['id' => $params['id']];
        $data = ActiveCouponLink::getDataOne($condition, '', []);
        $data['active_coupon'] = array_map('intval', explode(',', $data['active_coupon']));
        $data['time'] = [date('Y-m-d H:i:s',$data['start_time']),date('Y-m-d H:i:s',$data['end_time']) ];
        return self::jsonSuccess($data);
    }

}
